//
//  ResultViewController.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/9/10.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "ResultViewController.h"

#import "ProductCell.h"

#import "DetailViewController.h"

@interface ResultViewController ()<UITableViewDataSource,UITableViewDelegate>


@property(nonatomic,strong)UITableView * tableView;

@property(nonatomic,strong)NSMutableArray * productArray;

@property(nonatomic,copy)NSString * keyword;

@property(nonatomic,strong)ResultModel * resModel;



@end

@implementation ResultViewController

-(id)initWithKeyword:(NSString *)keyword{
    if(self = [super init]){
        self.keyword = keyword;
        self.productArray = [NSMutableArray array];
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    
    [[LXDataManager manager] getProductsWithKeyword:self.keyword complete:^(BOOL success, id res) {
        if(success){
            self.resModel = [res firstObject];
            [self.productArray removeAllObjects];
            [self.productArray addObjectsFromArray:self.resModel.produdtcArray];
            [self.tableView reloadData];
        }
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)setUpNavi{
    [super setUpNavi];
    self.navigationItem.title = [NSString stringWithFormat:@"\"%@\"的搜索结果",self.keyword];
}

-(void)loadMoreData{
    if(self.resModel.nextpageurl){
        [[LXDataManager manager] getProductsWithUrl:self.resModel.nextpageurl complete:^(BOOL success, id res) {
            if(success){
                self.resModel = [res firstObject];
                [self.productArray addObjectsFromArray:self.resModel.produdtcArray];
                [self.tableView reloadData];
            }
        }];
        [self.tableView.mj_footer endRefreshing];
    }else{
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


-(UITableView*)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = HexColor(@"#EEEEEE");
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.contentInset = UIEdgeInsetsMake(39, 0, 0, 0);
        _tableView.sectionFooterHeight = globalMargin;
        _tableView.sectionHeaderHeight = 0;
        [_tableView registerClass:[ProductCell class] forCellReuseIdentifier:@"ProductCell"];
        MJRefreshAutoNormalFooter * mj = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self loadMoreData];
        }];
        _tableView.mj_footer = mj;
        
    }
    return _tableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.productArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ProductCell"];
    if(self.productArray.count!=0){
        cell.product = self.productArray[indexPath.section];
    }
    cell.roundStyle =  RoundStyleAll;
    [self addCellHeight:cell.cellHeight indexPath:indexPath];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self cellHeightForIndexPath:indexPath];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    CGRect rect = [cell.productImageView convertRect:cell.productImageView.frame toView:self.view.window];
    
    CGRect rect1 = [cell.contentView convertRect:cell.contentView.frame toView:self
                    .view.window];
    CGPoint leftTop = CGPointMake(rect.origin.x>100?rect.origin.x:rect.origin.x, rect1.origin.y);
    //    NSLog(@"左上角%@ 按钮frame:%@",NSStringFromCGPoint(leftTop),NSStringFromCGRect(btn.frame));
    DetailViewController * detailVC = [[DetailViewController alloc] init];
    detailVC.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(leftTop.x, leftTop.y, cell.productImageView.width, cell.productImageView.width)];
    detailVC.imageView.image = cell.productImageView.image;
    detailVC.starPoint = leftTop;
    detailVC.product = [self.productArray objectAtIndex:indexPath.section];
    CircleAnimator * animator = [CircleAnimator animatorWithFromImageView:cell.productImageView toImageView:detailVC.imageView];
    detailVC.animator = animator;
    [self.navigationController xw_pushViewController:detailVC withAnimator:animator];
    
}
@end
